// Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
// License: GNU General Public License v3. See license.txt

frappe.query_reports["Profit and Loss Statement CN"] = $.extend({}, erpnext.financial_statements);

erpnext.utils.add_dimensions("Profit and Loss Statement CN", 10);

frappe.query_reports["Profit and Loss Statement CN"]["filters"].push({
    fieldname: "selected_view",
    label: __("Select View"),
    fieldtype: "Select",
    options: [
        { value: "Report", label: __("Report View") },
        { value: "Growth", label: __("Growth View") },
    ],
    default: "Report",
    reqd: 1,
});
frappe.query_reports["Profit and Loss Statement CN"]["filters"].push({
    fieldname: "accumulated_values",
    label: __("Accumulated Values"),
    fieldtype: "Check",
    default: 1,
});
frappe.query_reports["Profit and Loss Statement CN"]["filters"].push({
    fieldname: "include_default_book_entries",
    label: __("Include Default FB Entries"),
    fieldtype: "Check",
    default: 1,
});
frappe.query_reports["Profit and Loss Statement CN"]["filters"].push({
    fieldname: "last_fiscal_year",
    label: __("Show Last Fiscal Year Data"),
    fieldtype: "Check",
    default: 0,
    description: __("Add columns with data from the same period of last fiscal year"),
    hidden: true,
    depends_on: 'eval:doc.filter_based_on === "Date Range"'
});
frappe.query_reports["Profit and Loss Statement CN"]["filters"].push({
    fieldname: "profit_loss_template",
    label: __("Template"),
    fieldtype: "Link",
    options: "Profit Loss Config CN",
    description: __("Select template to apply account-reporting item mapping and sorting"),
    get_query: function() {
        return {
            filters: {
                company: frappe.query_report.get_filter_value("company")
            }
        };
    },
    on_change: function(query_report) {
        query_report.refresh();
    }
});

// 监听过滤方式变化，控制去年同期选项的显示/隐藏
frappe.query_reports["Profit and Loss Statement CN"].on("filter_change", function(query_report) {
    const filterBasedOn = query_report.get_filter_value("filter_based_on");
    query_report.toggle_filter_display("last_fiscal_year", filterBasedOn === "Date Range");
    
    // 当勾选状态变化时刷新报表
    const lastFiscalYear = query_report.get_filter_value("last_fiscal_year");
    query_report.refresh();
});
    